package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    //查询课程列表
    @Override
    public Object list(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //1.设置PageHelper参数分页
        PageHelper.startPage(page,pageSize);
        //2.调用Mapper方法进行查询
        List<Course> courseList = courseMapper.list(name,subject,target);
        //3.解析并封装结果
        Page<Course> p = (Page<Course>)courseList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //删除课程
    @Override
    public void deleteById(Integer id) {
        //调用Mapper执行删除
        courseMapper.deleteById(id);
    }
    //添加课程
    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法添加
        courseMapper.insert(course);
    }
    //根据id查询课程
    @Override
    public Course getById(Integer id) {
        //调用Mapper方法查询
        return courseMapper.selectById(id);
    }
    //修改课程
    @Override
    public void update(Course course) {
        //补全更新时间属性
        course.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法更新
        courseMapper.update(course);
    }
    //根据课程类别查询课程
    @Override
    public List<Course> listBySubject(Integer subject) {
        //调用Mapper方法查询
        List<Course> courseList = courseMapper.listBySubject(subject);
        return courseList;
    }
    //查询所有课程
    @Override
    public List<Course> listAll() {
        //调用Mapper方法查询
        List<Course> courseList = courseMapper.listAll();
        return courseList;
    }
}
